package com.gnuc.thoth.app.screens;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.gnuc.thoth.R;
import com.gnuc.thoth.framework.Thoth;
import com.gnuc.thoth.framework.callbacks.ThothCallback;

public class DialogExPreference extends DialogPreference
{
	public DialogExPreference(Context oContext, AttributeSet attrs)
	{
		super(oContext, attrs);
	}
	
	@Override
	protected void onBindDialogView(final View view)
	{
		super.onBindDialogView(view);
		if (view.getId() == R.id.setup_gmail)
		{
			if (Thoth.Settings.APP_ENABLE_GoMAIL)
			{
				final Button button = (Button) view.findViewById(R.id.gmail_setup_bt);
				button.setText("Disconnect GMail");
				button.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Thoth.Settings.APP_ENABLE_GoMAIL = false;
						Thoth.Settings.write();
						button.setText("Connect GMail");
					}
				});
			}
			else
			{
				final Button button = (Button) view.findViewById(R.id.gmail_setup_bt);
				button.setText("Connect GMail");
				button.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Thoth.createThothAsContact(new ThothCallback()
						{
							@Override
							public void handle(boolean result)
							{
								if (result)
								{
									Thoth.Settings.APP_ENABLE_GoMAIL = true;
									Thoth.Settings.write();
									button.setText("Disconnect GMail");
								}
							}
						});
					}
				});
			}
		}
		else if (view.getId() == R.id.setup_gcal)
		{
			if (Thoth.Settings.APP_ENABLE_GoCAL)
			{
				final Button button = (Button) view.findViewById(R.id.calendar_setup_bt);
				button.setText("Disconnect Calendar");
				button.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						Thoth.Settings.APP_ENABLE_GoCAL = false;
						Thoth.Settings.write();
						button.setText("Connect Calendar");
					}
				});
			}
			else
			{
				final Button button = (Button) view.findViewById(R.id.calendar_setup_bt);
				final ListView calendarList = (ListView) view.findViewById(R.id.calendar_setup_list);
				button.setText("Connect Calendar");
				button.setOnClickListener(new View.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						button.setVisibility(View.INVISIBLE);
						((TextView) view.findViewById(R.id.calendar_setup_msg)).setText(R.string.st_calendar_msg_2);
						final HashMap<String, Integer> calendarMap = new HashMap<String, Integer>();
						final Uri CALENDAR = (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.ECLAIR_MR1) ? Uri.parse("content://com.android.calendar") : Uri.parse("content://calendar");
						Cursor c = null;
						try
						{
							c = Thoth.cx.getContentResolver().query(Uri.withAppendedPath(CALENDAR, "calendars"), new String[]{"_id", "displayname", "_sync_account", "_sync_account_type"}, null, null, "displayname ASC");
							while (c != null && c.moveToNext())
							{
								if (c.getString(3).equalsIgnoreCase("com.google") && c.getString(2).equalsIgnoreCase(Thoth.Settings.USR_DEVICE_ACCOUNT_EMAIL))
									calendarMap.put(c.getString(1), c.getInt(0));
							}
						}
						catch (IllegalArgumentException e)
						{
							Log.e(Thoth.TAG, "Calendars not available", e);
						}
						finally
						{
							if (c != null)
								c.close();
						}
						final List<String> calendarNames = new ArrayList<String>();
						for (String calendar : calendarMap.keySet())
							calendarNames.add(calendar);
						calendarList.setVisibility(View.VISIBLE);
						calendarList.setAdapter(new ArrayAdapter<String>(Thoth.cx, android.R.layout.simple_expandable_list_item_1, calendarNames));
						calendarList.setOnItemClickListener(new OnItemClickListener()
						{
							@Override
							public void onItemClick(AdapterView<?> parent, View view, int pos, long id)
							{
								if (calendarMap.containsKey(calendarNames.get(pos)))
								{
									Thoth.Settings.USR_DEVICE_ACCOUNT_CALENDAR = calendarMap.get(calendarNames.get(pos));
									Thoth.Settings.APP_ENABLE_GoCAL = true;
									Thoth.Settings.write();
									calendarList.setVisibility(View.GONE);
									button.setVisibility(View.VISIBLE);
									button.setText("Disconnect Calendar");
								}
							}
						});
					}
				});
			}
		}
	}
}
